package com.spark.util.client

import com.spark.util.core.{Borrow, Logging}
import org.neo4j.driver.{AuthTokens, Driver, GraphDatabase, Session}

class Neo4jClient(func:() => Driver) extends Borrow {

  lazy val driver: Driver = func()

  def useSession[A](execute: Session=> A): Unit = using(driver.session())(execute)

  // 删除某个标签下的所有节点(提前删除索引)
  def removeLabel(label: String): Unit = useSession(session=> session.run(s"MATCH (r:${label}) DETACH DELETE r"))

  // 移除节点
  def removeNode(): Unit = {}

  // 查找节点的关系

  // 移除节点间的关系
  def removeRelationship(): Unit = {}

  def close(): Unit = driver.close()
}

object Neo4jClient extends Logging {

  def apply(url:String, username:String, password:String): Neo4jClient = {
    val func = () => GraphDatabase.driver(url, AuthTokens.basic(username, password))
    new Neo4jClient(func)
  }

}
